//
//  XuanKeViewController.m
//  Yu01
//
//  Created by apple on 16/8/19.
//  Copyright © 2016年 蒋松涵. All rights reserved.
//

#import "XuanKeViewController.h"
#import "XuanKeCell.h"
#import "XuKeViewController.h"

#import "SchoolViewController.h"
#import "MyViewController.h"
#import "XuanModel.h"
#import "CourseViewController.h"


@interface XuanKeViewController ()
{
    UITableView * _tableView;
}

@property (nonatomic,strong)NSMutableArray * dataArray;

@property (nonatomic,retain) NSString *urlStr;
@property (nonatomic,retain) NSDictionary *urlParameters;

@end

@implementation XuanKeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"选课";
    
    
    _urlStr = @"http://www.100xsys.com.cn/school/app/electiveCourseInfo/listJson";
    _urlParameters = [[NSDictionary alloc]initWithObjectsAndKeys:@"xuesheng1",@"accoutInfo.loginName",@"123456789",@"accoutInfo.password", nil];
    
    //异步执行代码
    [self fecthDataFromNet:_urlStr parameters:_urlParameters];
    
    
    self.dataArray = [[NSMutableArray alloc]init];
    
//    CGFloat h = 736 / ScreenHeight;
//    CGFloat w = 414 / ScreenWidth;
    
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.rowHeight = 140;
    [self.view addSubview:_tableView];
    
    [_tableView registerNib:[UINib nibWithNibName:@"XuanKeCell" bundle:nil] forCellReuseIdentifier:@"cell"];
}

#pragma mark - 数据请求
//请求数据
- (void)fecthDataFromNet:(NSString *)url parameters:(NSDictionary *)parameter {
    
    NSLog(@"%@",url);
    NSLog(@"%@",parameter);
    //初始化请求工具
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager POST:url parameters:parameter progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        
        NSLog(@"%@",dic);
        NSMutableArray * List = dic[@"rows"];
        
        for (NSDictionary * dicList in List)
        {
            XuanModel * model = [[XuanModel alloc]init];
            [model setValuesForKeysWithDictionary:dicList];
            [self.dataArray addObject:model];
            NSLog(@"%d",self.dataArray.count);
        }
        [_tableView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        
        
    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    XuanKeCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell)
    {
        cell = [[XuanKeCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    
    XuanModel * model = self.dataArray[indexPath.row];
    
    cell.nameLabel.text = model.courseName;
    cell.peopleLabel.text = model.teacherName;
    cell.jiaoShiLabel.text = [NSString stringWithFormat:@"%@ %ld-%ld",model.buildingName,model.floor,model.classroomNumber];
    cell.fanWeiLabel.text = model.classroomType;
    cell.xueFenLabel.text = [NSString stringWithFormat:@"%ld 学分",model.credit];
    
    
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    XuanModel * model = self.dataArray[indexPath.row];
    
        self.hidesBottomBarWhenPushed=YES;
        //
        CourseViewController * VC = [[CourseViewController alloc]init];
    
    VC.XKURL = [NSString stringWithFormat:@"http://www.100xsys.com.cn/school/app/electiveCourseInfo/getInfoJson?electiveCourseInfo.id=%@",model.id];
    
    VC.KeChengName = model.courseName;
    VC.LouName = model.buildingName;
    VC.RoomName = model.classroomType;
    VC.FangName = model.classroomNumber;
    VC.LouCeng = model.floor;
    VC.kecheng = model.classTime;
    VC.XueFen = model.credit;
    VC.Teacher = model.teacherName;
    VC.KCJJ = model.courseContent;
    
    [self.navigationController pushViewController:VC animated:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
